// Copyright 2019 Google, LLC.  All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: prod-OptionalExpression
description: >
  optional chain RHS of for await statement
info: |
  IterationStatement
    for await (LeftHandSideExpression of AssignmentExpression) Statement
features: [optional-chaining]
flags: [async]
---*/
function Test() {
    const obj = {
      iterable: {
        [Symbol.asyncIterator]() {
          return {
            i: 0,
            next() {
              if (this.i < 3) {
                return Promise.resolve({ value: this.i++, done: false });
              }
              return Promise.resolve({ done: true });
            }
          };
        }
      }
    };
    async function checkAssertions() {
      let count = 0;
      for await (const num of obj?.iterable) {
        count += num;
      }
      assert.sameValue(3, count);
    }
    checkAssertions().then($DONE, $DONE);
}

// preheat
const NUMBER_OF_PREHEAT = 1;
for (let i = 0; i < NUMBER_OF_PREHEAT; i++) {
    Test();
}

ArkTools.jitCompileAsync(Test);
assert.sameValue(ArkTools.waitJitCompileFinish(Test), true, '[JIT] JIT disabled.');

// test
const NUMBER_OF_TEST = 1; // or 2
for (let i = 0; i < NUMBER_OF_TEST; i++) {
    Test();
}
